import i18n from '@/i18n'
import { MetaResourceChartExampleDao } from '@apps/meta/domain/meta-resource-chart-example/meta-resource-chart-example.dao'
import { Assist } from '@libs/lhy-common'
import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { MetaResourceChartExampleSaveDto } from './dto/meta-resource-chart-example-save.dto'
import { MetaResourceChartExampleUpdateDto } from './dto/meta-resource-chart-example-update.dto'
import { MetaResourceChartExampleTreeVo } from './vo/meta-resource-chart-example-tree.vo'

@ApiTags(i18n.t('app-meta.meta-resource-chart-example.meta-resource-chart-example.12151147-0'))
@Controller({
  path: 'resource_chart_example',
  version: '1',
})
export class MetaResourceChartExampleController {
  constructor(private readonly resourceChartExampleDao: MetaResourceChartExampleDao) {}

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-chart-example.meta-resource-chart-example.12151147-1') })
  @Get('tree')
  async tree() {
    const list = await this.resourceChartExampleDao.list({})

    const vos: MetaResourceChartExampleTreeVo[] = []
    const voMap = new Map<string, MetaResourceChartExampleTreeVo>()
    list.forEach(data => {
      if (voMap.has(data.groupLabel)) {
        voMap.get(data.groupLabel)?.examples.push(data)
      } else {
        const vo = new MetaResourceChartExampleTreeVo()
        vo.id = vos.length + 1
        vo.groupLabel = data.groupLabel
        vo.examples = [data]
        vos.push(vo)
        voMap.set(data.groupLabel, vo)
      }
    })

    return vos
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-chart-example.meta-resource-chart-example.12151147-2') })
  @Get('list_group')
  async listGroup() {
    return this.resourceChartExampleDao.listGroup()
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-chart-example.meta-resource-chart-example.12151147-3') })
  @Put('save')
  async save(@Body() param: MetaResourceChartExampleSaveDto) {
    Assist.threw(
      await this.resourceChartExampleDao.isLabelExists([
        {
          label: param.label,
          groupLabel: param.groupLabel,
        },
      ]),
      i18n.t('app-meta.meta-resource-chart-example.meta-resource-chart-example.12151147-4', { args: [param.label] })
    )

    await this.resourceChartExampleDao.save({
      ...param,
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-chart-example.meta-resource-chart-example.12151147-5') })
  @Post('update')
  async update(@Body() param: MetaResourceChartExampleUpdateDto) {
    Assist.threw(
      await this.resourceChartExampleDao.isLabelExists([{ ...param }]),
      i18n.t('app-meta.meta-resource-chart-example.meta-resource-chart-example.12151147-4', { args: [param.label] })
    )

    await this.resourceChartExampleDao.updateById(param.id, param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-chart-example.meta-resource-chart-example.12151147-6') })
  @Delete('del')
  async del(@Query('id') id: number) {
    await this.resourceChartExampleDao.deleteById(id)
  }
}
